﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;

/// <summary>
/// Summary description for clsFacebookCount
/// </summary>
public class clsFacebookCount
{
    public string Url { get; set; }
    public string SharedCount { get; set; }
    public string LikeCount { get; set; }
    public string CommentCount { get; set; }
    public string ClickCount { get; set; }
    public string TotalCount { get; set; }

	public clsFacebookCount()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    /// <summary>
    /// Đếm các thông số trên facebook
    /// Author: ThuanNH
    /// </summary>
    /// <param name="urltxt"></param>
    /// <returns></returns>
    public static clsReturnCountFacebook[] Count(string urltxt)
    {
        List<clsReturnCountFacebook> details = new List<clsReturnCountFacebook>();
        WebClient web = new WebClient();
        string url = string.Format("https://api.facebook.com/method/fql.query?query=SELECT url, share_count, like_count, comment_count, total_count, click_count FROM link_stat where url='" + urltxt + "'");
        string response = web.DownloadString(url);
        DataSet ds = new DataSet();
        using (StringReader stringReader = new StringReader(response))
        {
            ds = new DataSet();
            ds.ReadXml(stringReader);
        }
        DataTable dt = ds.Tables["link_stat"];
        foreach (DataRow dtrow in dt.Rows)
        {
            clsReturnCountFacebook website = new clsReturnCountFacebook();
            website.Url = dtrow["url"].ToString();
            website.LikeCount = dtrow["like_count"].ToString();
            website.SharedCount = dtrow["share_count"].ToString();
            website.CommentCount = dtrow["comment_count"].ToString();
            website.ClickCount = dtrow["click_count"].ToString();
            website.TotalCount = dtrow["total_count"].ToString();
            details.Add(website);
        }
        return details.ToArray();
    }
}

public class clsReturnCountFacebook
{
    public string Url { get; set; }
    public string SharedCount { get; set; }
    public string LikeCount { get; set; }
    public string CommentCount { get; set; }
    public string ClickCount { get; set; }
    public string TotalCount { get; set; }
}